import axios from "axios"
// const debug = process.env.NODE_ENV !== 'production'
const instance = axios.create({
    baseURL: "https://api.freepandanovel.com/v1/",
    // baseURL: debug ? 'api' : "https://api.hidreamnovel.com/v2/",
    // baseURL: debug ? 'api' : "https://api.freepandanovel.com/v1/",
    timeout: 10000,
})

// 请求拦截器
instance.interceptors.request.use(config => {
  let token
    if (process.client) {
      token = localStorage.getItem("token");
    }
    if (token) {
        config.headers = config.headers || {};
        // config.headers.Authorization = `JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxMDAwMTU3LCJ1c2VybmFtZSI6Ijk1MDgyZGU3NjM1ZDQyYWE5OTFmY2Q0OTdjNDUzZjFjIiwiZXhwIjoxNjg4MjkyMTY2LCJlbWFpbCI6bnVsbH0.vYQEkvk6HE7QWZ6Pn8LswWGCMSTe_F4OCfiznYPhquU`;
        config.headers.Authorization = `JWT ${token}`;
    }
    return config
}, err => {
    return Promise.reject(err)
})

// // 响应拦截器
instance.interceptors.response.use(res => {
    if(res.data.code === 401){
        if (process.client)
          localStorage.removeItem('token')
    }
    return res.data
}, err => {
    return Promise.reject(err)
})
export default instance
